<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>bezier.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_bezier.js.html">bezier.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview Represents a cubic Bezier curve.
<a name="line18"></a> *
<a name="line19"></a> * Uses the deCasteljau algorithm to compute points on the curve.
<a name="line20"></a> * http://en.wikipedia.org/wiki/De_Casteljau&#39;s_algorithm
<a name="line21"></a> *
<a name="line22"></a> * Currently it uses an unrolled version of the algorithm for speed.  Eventually
<a name="line23"></a> * it may be useful to use the loop form of the algorithm in order to support
<a name="line24"></a> * curves of arbitrary degree.
<a name="line25"></a> *
<a name="line26"></a> * @author robbyw@google.com (Robby Walker)
<a name="line27"></a> * @author wcrosby@google.com (Wayne Crosby)
<a name="line28"></a> */
<a name="line29"></a>
<a name="line30"></a>goog.provide(&#39;goog.math.Bezier&#39;);
<a name="line31"></a>
<a name="line32"></a>goog.require(&#39;goog.math&#39;);
<a name="line33"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Object representing a cubic bezier curve.
<a name="line39"></a> * @param {number} x0 X coordinate of the start point.
<a name="line40"></a> * @param {number} y0 Y coordinate of the start point.
<a name="line41"></a> * @param {number} x1 X coordinate of the first control point.
<a name="line42"></a> * @param {number} y1 Y coordinate of the first control point.
<a name="line43"></a> * @param {number} x2 X coordinate of the second control point.
<a name="line44"></a> * @param {number} y2 Y coordinate of the second control point.
<a name="line45"></a> * @param {number} x3 X coordinate of the end point.
<a name="line46"></a> * @param {number} y3 Y coordinate of the end point.
<a name="line47"></a> * @constructor
<a name="line48"></a> */
<a name="line49"></a>goog.math.Bezier = function(x0, y0, x1, y1, x2, y2, x3, y3) {
<a name="line50"></a>  /**
<a name="line51"></a>   * X coordinate of the first point.
<a name="line52"></a>   * @type {number}
<a name="line53"></a>   */
<a name="line54"></a>  this.x0 = x0;
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * Y coordinate of the first point.
<a name="line58"></a>   * @type {number}
<a name="line59"></a>   */
<a name="line60"></a>  this.y0 = y0;
<a name="line61"></a>
<a name="line62"></a>  /**
<a name="line63"></a>   * X coordinate of the first control point.
<a name="line64"></a>   * @type {number}
<a name="line65"></a>   */
<a name="line66"></a>  this.x1 = x1;
<a name="line67"></a>
<a name="line68"></a>  /**
<a name="line69"></a>   * Y coordinate of the first control point.
<a name="line70"></a>   * @type {number}
<a name="line71"></a>   */
<a name="line72"></a>  this.y1 = y1;
<a name="line73"></a>
<a name="line74"></a>  /**
<a name="line75"></a>   * X coordinate of the second control point.
<a name="line76"></a>   * @type {number}
<a name="line77"></a>   */
<a name="line78"></a>  this.x2 = x2;
<a name="line79"></a>
<a name="line80"></a>  /**
<a name="line81"></a>   * Y coordinate of the second control point.
<a name="line82"></a>   * @type {number}
<a name="line83"></a>   */
<a name="line84"></a>  this.y2 = y2;
<a name="line85"></a>
<a name="line86"></a>  /**
<a name="line87"></a>   * X coordinate of the end point.
<a name="line88"></a>   * @type {number}
<a name="line89"></a>   */
<a name="line90"></a>  this.x3 = x3;
<a name="line91"></a>
<a name="line92"></a>  /**
<a name="line93"></a>   * Y coordinate of the end point.
<a name="line94"></a>   * @type {number}
<a name="line95"></a>   */
<a name="line96"></a>  this.y3 = y3;
<a name="line97"></a>};
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * Constant used to approximate ellipses.
<a name="line102"></a> * See: http://canvaspaint.org/blog/2006/12/ellipse/
<a name="line103"></a> * @type {number}
<a name="line104"></a> */
<a name="line105"></a>goog.math.Bezier.KAPPA = 4 * (Math.sqrt(2) - 1) / 3;
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * @return {!goog.math.Bezier} A copy of this curve.
<a name="line110"></a> */
<a name="line111"></a>goog.math.Bezier.prototype.clone = function() {
<a name="line112"></a>  return new goog.math.Bezier(this.x0, this.y0, this.x1, this.y1, this.x2,
<a name="line113"></a>      this.y2, this.x3, this.y3);
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Test if the given curve is exactly the same as this one.
<a name="line119"></a> * @param {goog.math.Bezier} other The other curve.
<a name="line120"></a> * @return {boolean} Whether the given curve is the same as this one.
<a name="line121"></a> */
<a name="line122"></a>goog.math.Bezier.prototype.equals = function(other) {
<a name="line123"></a>  return this.x0 == other.x0 &amp;&amp; this.y0 == other.y0 &amp;&amp; this.x1 == other.x1 &amp;&amp;
<a name="line124"></a>         this.y1 == other.y1 &amp;&amp; this.x2 == other.x2 &amp;&amp; this.y2 == other.y2 &amp;&amp;
<a name="line125"></a>         this.x3 == other.x3 &amp;&amp; this.y3 == other.y3;
<a name="line126"></a>};
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Modifies the curve in place to progress in the opposite direction.
<a name="line131"></a> */
<a name="line132"></a>goog.math.Bezier.prototype.flip = function() {
<a name="line133"></a>  var temp = this.x0;
<a name="line134"></a>  this.x0 = this.x3;
<a name="line135"></a>  this.x3 = temp;
<a name="line136"></a>  temp = this.y0;
<a name="line137"></a>  this.y0 = this.y3;
<a name="line138"></a>  this.y3 = temp;
<a name="line139"></a>
<a name="line140"></a>  temp = this.x1;
<a name="line141"></a>  this.x1 = this.x2;
<a name="line142"></a>  this.x2 = temp;
<a name="line143"></a>  temp = this.y1;
<a name="line144"></a>  this.y1 = this.y2;
<a name="line145"></a>  this.y2 = temp;
<a name="line146"></a>};
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Computes the curve at a point between 0 and 1.
<a name="line151"></a> * @param {number} t The point on the curve to find.
<a name="line152"></a> * @return {!goog.math.Coordinate} The computed coordinate.
<a name="line153"></a> */
<a name="line154"></a>goog.math.Bezier.prototype.getPoint = function(t) {
<a name="line155"></a>  // Special case start and end
<a name="line156"></a>  if (t == 0) {
<a name="line157"></a>    return new goog.math.Coordinate(this.x0, this.y0);
<a name="line158"></a>  } else if (t == 1) {
<a name="line159"></a>    return new goog.math.Coordinate(this.x3, this.y3);
<a name="line160"></a>  }
<a name="line161"></a>
<a name="line162"></a>  // Step one - from 4 points to 3
<a name="line163"></a>  var ix0 = goog.math.lerp(this.x0, this.x1, t);
<a name="line164"></a>  var iy0 = goog.math.lerp(this.y0, this.y1, t);
<a name="line165"></a>
<a name="line166"></a>  var ix1 = goog.math.lerp(this.x1, this.x2, t);
<a name="line167"></a>  var iy1 = goog.math.lerp(this.y1, this.y2, t);
<a name="line168"></a>
<a name="line169"></a>  var ix2 = goog.math.lerp(this.x2, this.x3, t);
<a name="line170"></a>  var iy2 = goog.math.lerp(this.y2, this.y3, t);
<a name="line171"></a>
<a name="line172"></a>  // Step two - from 3 points to 2
<a name="line173"></a>  ix0 = goog.math.lerp(ix0, ix1, t);
<a name="line174"></a>  iy0 = goog.math.lerp(iy0, iy1, t);
<a name="line175"></a>
<a name="line176"></a>  ix1 = goog.math.lerp(ix1, ix2, t);
<a name="line177"></a>  iy1 = goog.math.lerp(iy1, iy2, t);
<a name="line178"></a>
<a name="line179"></a>  // Final step - last point
<a name="line180"></a>  return new goog.math.Coordinate(goog.math.lerp(ix0, ix1, t),
<a name="line181"></a>      goog.math.lerp(iy0, iy1, t));
<a name="line182"></a>};
<a name="line183"></a>
<a name="line184"></a>
<a name="line185"></a>/**
<a name="line186"></a> * Changes this curve in place to be the portion of itself from [t, 1].
<a name="line187"></a> * @param {number} t The start of the desired portion of the curve.
<a name="line188"></a> */
<a name="line189"></a>goog.math.Bezier.prototype.subdivideLeft = function(t) {
<a name="line190"></a>  if (t == 1) {
<a name="line191"></a>    return;
<a name="line192"></a>  }
<a name="line193"></a>
<a name="line194"></a>  // Step one - from 4 points to 3
<a name="line195"></a>  var ix0 = goog.math.lerp(this.x0, this.x1, t);
<a name="line196"></a>  var iy0 = goog.math.lerp(this.y0, this.y1, t);
<a name="line197"></a>
<a name="line198"></a>  var ix1 = goog.math.lerp(this.x1, this.x2, t);
<a name="line199"></a>  var iy1 = goog.math.lerp(this.y1, this.y2, t);
<a name="line200"></a>
<a name="line201"></a>  var ix2 = goog.math.lerp(this.x2, this.x3, t);
<a name="line202"></a>  var iy2 = goog.math.lerp(this.y2, this.y3, t);
<a name="line203"></a>
<a name="line204"></a>  // Collect our new x1 and y1
<a name="line205"></a>  this.x1 = ix0;
<a name="line206"></a>  this.y1 = iy0;
<a name="line207"></a>
<a name="line208"></a>  // Step two - from 3 points to 2
<a name="line209"></a>  ix0 = goog.math.lerp(ix0, ix1, t);
<a name="line210"></a>  iy0 = goog.math.lerp(iy0, iy1, t);
<a name="line211"></a>
<a name="line212"></a>  ix1 = goog.math.lerp(ix1, ix2, t);
<a name="line213"></a>  iy1 = goog.math.lerp(iy1, iy2, t);
<a name="line214"></a>
<a name="line215"></a>  // Collect our new x2 and y2
<a name="line216"></a>  this.x2 = ix0;
<a name="line217"></a>  this.y2 = iy0;
<a name="line218"></a>
<a name="line219"></a>  // Final step - last point
<a name="line220"></a>  this.x3 = goog.math.lerp(ix0, ix1, t);
<a name="line221"></a>  this.y3 = goog.math.lerp(iy0, iy1, t);
<a name="line222"></a>};
<a name="line223"></a>
<a name="line224"></a>
<a name="line225"></a>/**
<a name="line226"></a> * Changes this curve in place to be the portion of itself from [0, t].
<a name="line227"></a> * @param {number} t The end of the desired portion of the curve.
<a name="line228"></a> */
<a name="line229"></a>goog.math.Bezier.prototype.subdivideRight = function(t) {
<a name="line230"></a>  this.flip();
<a name="line231"></a>  this.subdivideLeft(1 - t);
<a name="line232"></a>  this.flip();
<a name="line233"></a>};
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>/**
<a name="line237"></a> * Changes this curve in place to be the portion of itself from [s, t].
<a name="line238"></a> * @param {number} s The start of the desired portion of the curve.
<a name="line239"></a> * @param {number} t The end of the desired portion of the curve.
<a name="line240"></a> */
<a name="line241"></a>goog.math.Bezier.prototype.subdivide = function(s, t) {
<a name="line242"></a>  this.subdivideRight(s);
<a name="line243"></a>  this.subdivideLeft((t - s) / (1 - s));
<a name="line244"></a>};
<a name="line245"></a>
<a name="line246"></a>
<a name="line247"></a>/**
<a name="line248"></a> * Computes the position t of a point on the curve given its x coordinate.
<a name="line249"></a> * That is, for an input xVal, finds t s.t. getPoint(t).x = xVal.
<a name="line250"></a> * As such, the following should always be true up to some small epsilon:
<a name="line251"></a> * t ~ solvePositionFromXValue(getPoint(t).x) for t in [0, 1].
<a name="line252"></a> * @param {number} xVal The x coordinate of the point to find on the curve.
<a name="line253"></a> * @return {number} The position t.
<a name="line254"></a> */
<a name="line255"></a>goog.math.Bezier.prototype.solvePositionFromXValue = function(xVal) {
<a name="line256"></a>  // Desired precision on the computation.
<a name="line257"></a>  var epsilon = 1e-6;
<a name="line258"></a>
<a name="line259"></a>  // Initial estimate of t using linear interpolation.
<a name="line260"></a>  var t = (xVal - this.x0) / (this.x3 - this.x0);
<a name="line261"></a>  if (t &lt;= 0) {
<a name="line262"></a>    return 0;
<a name="line263"></a>  } else if (t &gt;= 1) {
<a name="line264"></a>    return 1;
<a name="line265"></a>  }
<a name="line266"></a>
<a name="line267"></a>  // Try gradient descent to solve for t. If it works, it is very fast.
<a name="line268"></a>  var tMin = 0;
<a name="line269"></a>  var tMax = 1;
<a name="line270"></a>  for (var i = 0; i &lt; 8; i++) {
<a name="line271"></a>    var value = this.getPoint(t).x;
<a name="line272"></a>    var derivative = (this.getPoint(t + epsilon).x - value) / epsilon;
<a name="line273"></a>    if (Math.abs(value - xVal) &lt; epsilon) {
<a name="line274"></a>      return t;
<a name="line275"></a>    } else if (Math.abs(derivative) &lt; epsilon) {
<a name="line276"></a>      break;
<a name="line277"></a>    } else {
<a name="line278"></a>      if (value &lt; xVal) {
<a name="line279"></a>        tMin = t;
<a name="line280"></a>      } else {
<a name="line281"></a>        tMax = t;
<a name="line282"></a>      }
<a name="line283"></a>      t -= (value - xVal) / derivative;
<a name="line284"></a>    }
<a name="line285"></a>  }
<a name="line286"></a>
<a name="line287"></a>  // If the gradient descent got stuck in a local minimum, e.g. because
<a name="line288"></a>  // the derivative was close to 0, use a Dichotomy refinement instead.
<a name="line289"></a>  // We limit the number of interations to 8.
<a name="line290"></a>  for (var i = 0; Math.abs(value - xVal) &gt; epsilon &amp;&amp; i &lt; 8; i++) {
<a name="line291"></a>    if (value &lt; xVal) {
<a name="line292"></a>      tMin = t;
<a name="line293"></a>      t = (t + tMax) / 2;
<a name="line294"></a>    } else {
<a name="line295"></a>      tMax = t;
<a name="line296"></a>      t = (t + tMin) / 2;
<a name="line297"></a>    }
<a name="line298"></a>    value = this.getPoint(t).x;
<a name="line299"></a>  }
<a name="line300"></a>  return t;
<a name="line301"></a>};
<a name="line302"></a>
<a name="line303"></a>
<a name="line304"></a>/**
<a name="line305"></a> * Computes the y coordinate of a point on the curve given its x coordinate.
<a name="line306"></a> * @param {number} xVal The x coordinate of the point on the curve.
<a name="line307"></a> * @return {number} The y coordinate of the point on the curve.
<a name="line308"></a> */
<a name="line309"></a>goog.math.Bezier.prototype.solveYValueFromXValue = function(xVal) {
<a name="line310"></a>  return this.getPoint(this.solvePositionFromXValue(xVal)).y;
<a name="line311"></a>};
</pre>


</body>
</html>
